package com.baoxin.chenxixc4coach;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;

import com.baoxin.chenxixc4coach.adapter.ReferenceAdapter;
import com.baoxin.chenxixc4coach.bean.News;
import com.baoxin.chenxixc4coach.java.MyHandler;
import com.baoxin.chenxixc4coach.java.ToWebListener;
import com.baoxin.chenxixc4coach.utils.Interfaces;
import com.baoxin.chenxixc4coach.utils.Msg;
import com.baoxin.chenxixc4coach.utils.NetUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 教练内参
 */
public class CoachReferenceActivity extends BaseActivity implements ToWebListener {

    private RecyclerView recyclerView;
    private List<News> newsList;
    private ReferenceAdapter adapter;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coach_reference);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        recyclerView = findViewById(R.id.recyclerview);
        newsList = new ArrayList<>();
        adapter = new ReferenceAdapter(this, this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

        request();
    }

    private void request() {

        handler = new MyHandler(this, activity) {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case Msg.REQUEST_SUCCEED:
                        newsList = (List<News>) msg.obj;
                        adapter.setData(newsList);
                        adapter.notifyDataSetChanged();
                        break;

                }
            }
        };

        JSONObject body = new JSONObject();
        try {
            body.put("page", 1);
            body.put("limit", 10);
            body.put("lable", "教练内参");
            NetUtil.post(this, Interfaces.URL_HEAD_NEWS, body.toString(), new MyCallback(Interfaces.URL_HEAD_NEWS, handler));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void openWeb(String url) {
        H5TestActivity.startAction(this, url);
    }
}
